package DAO;

import java.util.List;

import metier.Film;
import metier.Note;

public class DAONoteJPA extends DAOJPA implements DAONote{

	private static DAONoteJPA instance=null;
	
	private DAONoteJPA() {}
	
	public static DAONoteJPA getInstance() {
		if(instance == null)
			instance = new DAONoteJPA();
		return instance;
	}

	
	@Override
	public Note get(int id) {
		Note personne = DAOJPA.getManager().find(Note.class, id);
		return personne;
	}

	@Override
	public void save(Note note) {
		if((note.getUser().getId() == -1)||(note.getFilm().getId() == -1))
			throw new IllegalArgumentException("Film ou utilisateur invalide");
		
		DAOJPA.getManager().persist(note);
		DAOJPA.commit();
		
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<Note> loadNotesFilm(Film f) {
		return  DAOJPA.getManager().createNativeQuery("SELECT * FROM note WHERE id_film IN (SELECT id_film ='"+f.getId()+"' FROM Film",Note.class).getResultList();
	}
	
}
